package com.zjw;

import com.zjw.entity.Goods;
import com.zjw.entity.Page;
import com.zjw.entity.TradeLog;
import com.zjw.service.GoodsService;
import com.zjw.service.UserService;
import com.zjw.service.impl.GoodsServiceImpl;
import com.zjw.service.impl.UserServiceImpl;
import com.zjw.utils.Constants;
import com.zjw.vo.GoodsDetail;

import java.util.Date;
import java.util.Objects;
import java.util.Scanner;

/**
 * @projectName: zhuanZhuan
 * @package: com.zjw
 * @className: CommonOperate
 * @author: 翟
 * @date: 2025/4/10 11:02
 */

public class CommonOperate {

    private static final Scanner input = new Scanner(System.in);

    /**
     * 查询商品详情
     *
     * @param loginType 登录类型
     * @return
     */
    private static GoodsDetail queryGoodsDetail(String loginType) {
        System.out.println("请输入输入要查看的好物编号：");
        int goodsId = input.nextInt();
        GoodsService goodsService = new GoodsServiceImpl();
        GoodsDetail goodsDetail = goodsService.queryGoodsDetailById(goodsId, loginType);
        if (goodsDetail != null) {
            System.out.println("好物名称：" + goodsDetail.getGoodsName());
            System.out.println("好物类型：" + goodsDetail.getTypeName());
            System.out.println("好物价格：" + goodsDetail.getGoodsPrice());
            System.out.println("好物主人：" + goodsDetail.getUserName());
            System.out.println("好物介绍：" + goodsDetail.getGoodsIntroduce());
            System.out.println("上架时间：" + goodsDetail.getGoodsAddTime());
        } else {
            System.out.println("没有找到该商品");
        }
        return goodsDetail;
    }

    /**
     * 获取好物列表
     */
    public static void getGoodsList(String loginType , int userId) {
        GoodsService goodsService = new GoodsServiceImpl();
        Page page = new Page();
        page.setTotalCount(goodsService.queryGoodsCountByLoginType(loginType));
        do {
            System.out.println("编号\t\t名称\t\t价格\t\t上架时间");
            for (Goods goods : goodsService.queryGoodsByLoginTypeAndPaging(page.getCurrPageNo(), page.getPageSize(), loginType)) {
                System.out.println(goods.getGoodsId() + "\t\t" + goods.getGoodsName() + "\t\t" + goods.getGoodsPrice() + "\t\t" + goods.getGoodsAddTime());
            }
            System.out.println("页数/总页数：" + page.getCurrPageNo() + "/" + page.getTotalPageCount());
            System.out.println("1.查看好物详情 2.上一页 3.下一页 0.返回上一级");
            int choice = input.nextInt();
            switch (choice) {
                case 1:
                    GoodsDetail goodsDetail = queryGoodsDetail(loginType);
                    if (goodsDetail != null) {
                        if (Objects.equals(loginType, Constants.LOGIN_TYPE_USERNAME)) {
                            System.out.println("1.购买此好物，输入其他返回上一级");
                            int buyChoice = input.nextInt();
                            if (buyChoice == 1)
                                buyGoods(goodsDetail.getGoodsId(), userId, goodsDetail.getSellerId(), goodsDetail.getGoodsPrice());
                        }
                    }
                    break;
                case 2:
                    int currPageNo = page.getCurrPageNo();
                    page.setCurrPageNo(page.getCurrPageNo() - 1);
                    if (currPageNo == page.getCurrPageNo()) {
                        System.out.println("已是首页，不能再向前翻页");
                    }
                    break;
                case 3:
                    if (page.getCurrPageNo() + 1 > page.getTotalPageCount()) {
                        System.out.println("已是末页，不能再向后翻页");
                    }
                    page.setCurrPageNo(page.getCurrPageNo() + 1);
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
            System.out.println();
        } while (true);
    }

    /**
     * 购买好物
     */
    private static void buyGoods(int goodsID, int buyer_id, int seller_id, double price) {
        System.out.println("请输入收货信息：");
        String address = input.next().trim();
        TradeLog tradeLog = new TradeLog(null, buyer_id, goodsID, seller_id, new Date(), address);
        UserService userService = new UserServiceImpl();
        if (userService.modifyBalanceByUserId(price, tradeLog)) {
            System.out.println("购买成功");
        } else {
            System.out.println("购买失败");
        }
    }
}
